{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "JuliaRunClient for 17523648210908459309 @ http://juliarunremote-svc.juliarun:80"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using JuliaRunClient\n",
    "ctx = Context()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "JuliaRunClient.JuliaBatch(\"mywebserver\")"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "job = JuliaBatch(\"mywebserver\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# code for a simple webserver\n",
    "webserver_script = \"\"\"\n",
    "using HttpServer\n",
    "\n",
    "http = HttpHandler() do req::Request, res::Response\n",
    "    Response(\"JuliaRun says hello from \" * gethostname())\n",
    "end\n",
    "\n",
    "server = Server(http)\n",
    "run(server, 8000)        \n",
    "\"\"\"\n",
    "\n",
    "open(\"/mnt/juliabox/helloweb.jl\", \"w\") do f\n",
    "    println(f, webserver_script)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# start a webserver job, specifying port 8000 to be opened up\n",
    "@result submitJob(ctx, job; start_script=\"/mnt/juliabox/helloweb.jl\", run_volume=\"juliabox\", image=\"juliabox\", cpu=\"1\", memory=\"1Gi\", shell=\"/juliabox/scripts/master.sh\", ports=\"8000:8000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dict{String,Any} with 6 entries:\n",
       "  \"nworkers_succeeded\" => 0\n",
       "  \"npending\"           => 0\n",
       "  \"parallelism\"        => 0\n",
       "  \"created\"            => true\n",
       "  \"nuptodate\"          => 0\n",
       "  \"succeeded\"          => false"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check that the job has started\n",
    "@result getJobStatus(ctx, job)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Any,1}:\n",
       " \"10.0.180.127\"                                      \n",
       " Dict{String,Any}(Pair{String,Any}(\"port8000\", 8000))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get the IP and port that was assigned for the webserver to listen on.\n",
    "# The webserver may actually be running any of the physical nodes of the cluster.\n",
    "# And it is running in an isolated container, with its own virtual IP address\n",
    "# This IP address is accessible only to the user who started the webserver.\n",
    "ip, portspec = @result getJobEndpoint(ctx, job)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"http://10.0.180.127:8000/\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We can connect to it from here (or anything else run by this user)\n",
    "using Requests\n",
    "url = \"http://$(ip):$(first(values(portspec)))/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"JuliaRun says hello from mywebserver-zrjdq\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "String(Requests.bytes(get(url)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We stop the webserver now\n",
    "@result deleteJob(ctx, job; force=true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.2",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
